package cn.gson.fengboot.model.service;

import cn.gson.fengboot.controller.DbusinessController;
import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.DvieMapper;
import cn.gson.fengboot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DvieService {
    @Autowired
    DvieMapper dvieMapper;
    @Autowired
    DvieDao dvieDao;
    @Autowired
    DbusvieDao dbusvieDao;
    @Autowired
    DvieproDao dvieproDao;
    @Autowired
    DviepromodelDao dviepromodelDao;
    @Autowired
    DbvandvproDao dbvandvproDao;
    @Autowired
    DbusinessController controller;

    public List<Dbusvie> selectvie(Integer busId, HttpServletRequest request){
        String bviePeople = controller.getbuspeople(request);
        return dvieMapper.selectvie(busId,bviePeople);
    }

    public void addvie(Dbusvie dbusvie,List<Dbvandvpro> viepros) {
        Dbusvie save = dbusvieDao.save(dbusvie);
        System.out.println(save.getBvieId());
        addvieandpro(save,viepros);
    }

    public void addvieandpro(Dbusvie dbusvie,List<Dbvandvpro> viepros){
        JSONArray json = JSONArray.parseArray(String.valueOf(viepros)) ;
        JSONObject jsonObject = null;
        Integer bvieId = dbusvie.getBvieId();
        Dbvandvpro dbvandvpro = null;
        for (int i=0;i<json.size();i++){
            jsonObject = (JSONObject)json.get(i);//获取每一行转成JSON对象
            Dviepro vpros = jsonObject.getObject("vpros", Dviepro.class);
            System.out.println(vpros);
            dbvandvpro = new Dbvandvpro(bvieId,vpros);
            dbvandvproDao.save(dbvandvpro);
        }
    }

    public void detvie(Integer bvieId) {
        dbusvieDao.deleteById(bvieId);
    }

    public List<Dvie> selectallvie(String vieName) {
        return dvieMapper.selectallvie(vieName);
    }

    public List<Dviepro> selectallviepro(Integer vieId, String vproName) {
        return dvieMapper.selectallviepro(vieId,vproName);
    }

    public void addmyvies(Dvie dvie) {
        dvieDao.save(dvie);
    }

    public List<Dviepromodel> selectmodel() {
         return dvieMapper.selectmodel();
    }

    public void addviepro(Dviepro dviepro) {
        dvieproDao.save(dviepro);
    }

    public void trueaddvpromodel(Dviepromodel dviepromodel) {
        dviepromodelDao.save(dviepromodel);
    }

    public Dbusvie selectById(Integer bvieId) {
        return dbusvieDao.findById(bvieId).get();
    }

    public Integer selectBybvieName(String bvieName,HttpServletRequest request) {
        String bviePeople = controller.getbuspeople(request);
        List<Dbusvie> byBvieName = dbusvieDao.findByBvieName(bvieName, bviePeople);
        if(byBvieName.size()>0){
            return 1;

        }else{
            return 0;
        }

    }
}
